package com.etc.dao.impl;

import java.util.List;

import com.etc.dao.ShopCartDao;
import com.etc.entity.Goods;
import com.etc.entity.Shop;
import com.etc.entity.Shopc;
import com.etc.util.DBUtil;
import com.etc.util.PageData;

/**
 * 购物车dao实现类
 * 
 * @author
 *
 */
public class ShopCartDaoImpl implements ShopCartDao {
	/**
	 * 查询用户自己的全部购物车
	 */
	@Override
	public PageData<Shopc> selShopCar(int userid, int page, int pageSize, String keywords) {
		String sql = "select * from tbl_shopc where userid=? and goodsname like ?";
		PageData<Shopc> pd = DBUtil.exQueryByPage(sql, Shopc.class, page, pageSize, userid, "%" + keywords + "%");
		return pd;
	}

	/**
	 * 将商品加进购物车
	 */
	@Override
	public boolean addShopCar(Shopc shopc) {
		String sql = "insert into tbl_shopc values(null,?,?,?,?,?,?)";
		return DBUtil.exUpdate(sql, shopc.getUserid(), shopc.getGoodsid(), shopc.getShopid(), shopc.getGoodsname(),
				shopc.getShopccount(), shopc.getTotal()) > 0;

	}

	/**
	 * 删除购物车商品
	 */
	@Override
	public boolean delShopCar(int userid, String goodsname) {
		String sql = "delete from tbl_shopc where userid=? and goodsname=?";
		return DBUtil.exUpdate(sql, userid, goodsname) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: selShopName
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param goods
	 * @param goodsname
	 * @return
	 * 
	 * @see com.etc.zmall.dao.ShopCartDao#selShopName(com.etc.zmall.entity.Goods,
	 *      java.lang.String)
	 * 
	 */
	public Goods selShopName(Goods goods, String goodsname) {
		String sql = "select * from tbl_shop where shopid=(select shopid from tbl_goods where goodsname=?)";
		List<Shop> list = (List<Shop>) DBUtil.exQuery(sql, Shop.class, goodsname);
		if (list.size() == 0) {
			return null;
		}
		Goods g = new Goods(goods.getGoodsid(), goods.getGoodsname(), goods.getGoodsprice(), goods.getGoodsinfo(),
				goods.getGoodscount(), goods.getCover(), goods.getTypeid(), list.get(0).getShopname());
		return g;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: getShopcByUserIdAndGoodsId
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param userid
	 * @param goodsid
	 * @param shopid
	 * @return
	 * 
	 * @see com.etc.zmall.dao.ShopCartDao#getShopcByUserIdAndGoodsId(int, int, int)
	 * 
	 */
	@Override
	public Shopc getShopcByUserIdAndGoodsId(int userid, int goodsid, int shopid) {
		// TODO Auto-generated method stub
		String sql = "select * from tbl_shopc where userid=? and goodsid=? and shopid=?";
		List<Shopc> list = (List<Shopc>) DBUtil.exQuery(sql, Shopc.class, userid, goodsid, shopid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: updateShopcByUserIdAndGoodsId
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param shopc
	 * @return
	 * 
	 * @see com.etc.zmall.dao.ShopCartDao#updateShopcByUserIdAndGoodsId(com.etc.zmall.entity.Shopc)
	 * 
	 */
	@Override
	public boolean updateShopcByshopcid(Shopc shopc) {
		String sql = "update tbl_shopc set shopccount=shopccount+?,total=total+? where shopcid=?";
		return DBUtil.exUpdate(sql, shopc.getShopccount(), shopc.getTotal(), shopc.getShopcid()) > 0;

	}

}
